import 'dart:async';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_app/values/MyColors.dart';
import 'package:flutter_app/widget/custom_app_bar.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:webview_flutter/webview_flutter.dart';

/**
 * @Description: WebView 通用界面
 * @Author: 下雨了
 * @CreateDate: 2021-12-30 17:24
 * @UpdateUser: 更新者
 * @UpdateDate:  2021-12-30 17:24
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
///
class WebViewPage extends StatefulWidget {
  final String title; // 标题
  final String url; // 访问的地址
  const WebViewPage({Key? key, required this.title, required this.url}) : super(key: key);

  @override
  _WebViewPageState createState() => _WebViewPageState();
}

class _WebViewPageState extends State<WebViewPage> {
  final Completer<WebViewController> _controller = Completer<WebViewController>();

  late bool progressState = true;

  @override
  void initState() {
    super.initState();
    if (Platform.isAndroid) {
      WebView.platform = SurfaceAndroidWebView();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: MyColors.FFF1F1F1,
      appBar: MyAppBar(context, title: widget.title),
      body: Column(
        children: [
          Visibility(
            visible: progressState,
            child: LinearProgressIndicator(
              minHeight: 3.r,
              color: MyColors.FF2AAB33,
            ),
          ),
          Expanded(
            child: WebView(
              initialUrl: widget.url,
              javascriptMode: JavascriptMode.unrestricted,
              onPageFinished: (String url) {
                setState(() {
                  progressState = false;
                });
              },
            ),
          ),
        ],
      ),
    );
  }
}
